package capricornus.utils
{
	import flash.utils.ByteArray;
	
	public class StringUtils
	{
		/**
		 * 清除聊天里面的html内容，防止html内容注入 
		 * @param msg
		 * @return 
		 * 
		 */		
		public static function clearChatHtml( msg:String ):String
		{
			var i:int=0;
			while( (i=msg.indexOf("<")) != -1 )
			{
				msg = msg.replace( "<", "&lt;" );
			}
			return msg;
		}
		
		public static function clearHttpReqVars( url:String ):String
		{
			if( url==null ) return url;
			var index:int;
			if( (index=url.indexOf( "?" )) != -1 )
			{
				url = url.substr( 0, index );
			}
			return url;
		}
		
		public static function getStringLen(txt:String):uint
		{
			var txtBytes:ByteArray = new ByteArray();
			txtBytes.writeMultiByte(txt, 'utf-8');
			
			var len:uint = txtBytes.length;
			txtBytes.clear();
			
			return len;
		}
		
		public static function isImageURL( str:String ):Boolean
		{
			str = clearHttpReqVars( str );
			var s:String = str.substr( str.length-4 ).toLowerCase();
			return s==".jpg" || s==".png";
		}
		
		public static function getHtmlText( text:String, color:uint ):String
		{
			var str:String = "";
			str += '<font color="#';
			str += color.toString(16);
			str += '">';
			str += text;
			str += '</font>';
			
			return str;
		}
		
		public static function getLinkText( color:uint, linkCMD:String, linkText:String, showText:String ):String
		{
			var str:String = "";
			str += '<u><font color="#';
			str += color.toString(16);
			str += '"><a href="event:';
			str += linkCMD;
			str += "=";
			str += linkText;
			str += '">';
			str += showText;
			str += '</a></font></u>';
			
			return str;
		}
		
		public function StringUtils()
		{
		}
	}
}